package Year22_Month01.day0110;

public class Solution {
    /*
    给定一个头结点为 head 的非空单链表，返回链表的中间结点。

    如果有两个中间结点，则返回第二个中间结点。
     */
    public ListNode middleNode(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null){
            fast = fast.next;
            slow = slow.next;
            if(fast != null){
                fast = fast.next;
            }
        }
        return slow;
    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2){
        ListNode cur = new ListNode(1);
        ListNode key = cur;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
            } else {
                cur.next = l2;
            }
            cur = cur.next;
        }
        if (l1 == null) {
            cur.next = l2;
        }
        if (l2 == null) {
            cur.next = l1;
        }
        return key.next;
    }
}
